/*
 * Copyright 2012 ITC Tsinghua University. All Rights Reserved.
 */
package cn.edu.tsinghua.cic.ccss.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.forten.core.dao.rdb.query.parameter.Param;
import org.forten.core.dao.rdb.query.parameter.ParamHelper;
import org.forten.core.ro.PageInfo;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import cn.edu.tsinghua.cic.ccss.entity.Course;

/**
 * 
 * 
 * @author <a href="mailto:du_yi@bbn.cn">Duyi</a>
 * 
 * @since 2012-5-30
 */
@Repository("courseDao")
public class CourseDaoImpl {
	private HibernateTemplate hibernateTemplate;

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public Session getSession() {
		return hibernateTemplate.getSessionFactory().getCurrentSession();
	}

	public long getCount(String hql, Param... params) {
		Session session = getSession();
		Query query = session.createQuery(hql);
		query = ParamHelper.setParamsInQuery(query, params);
		long total = (Long) query.uniqueResult();
		return total;
	}

	@SuppressWarnings("unchecked")
	public List<Course> findCourse(String hql, PageInfo pi, Param... params) {
		Session session = getSession();
		Query query = session.createQuery(hql);
		query = ParamHelper.setParamsInQuery(query, params);
		query.setCacheable(true);
		query.setFirstResult(pi.getFirstResultNum());
		query.setMaxResults(pi.getPageSize());

		return query.list();
	}
}
